package ch.ethz.group3.myalbum.client;

import java.util.Collection;
import java.util.Random;

import ch.ethz.group3.myalbum.client.utils.Global;
import ch.ethz.group3.myalbum.client.utils.Notifier;
import ch.ethz.group3.myalbum.shared.GroupModel;
import ch.ethz.group3.myalbum.shared.InvalidFlickrTokenException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class GroupListPanel extends VLayout implements EnterListener {
	private final Random r;
	private final ListGrid groupGrid;
	private final GroupsDataSource ds;
	private final PictureServiceAsync pictureService;

	public GroupListPanel() {
		r = new Random();

		this.pictureService = GWT.create(PictureService.class);

		groupGrid = new ListGrid();
		groupGrid.setShowFilterEditor(true);
		groupGrid.setFilterOnKeypress(true);
		groupGrid.setShowRecordComponents(true);
		groupGrid.setShowRecordComponentsByCell(true);
		groupGrid.setShowAllRecords(false);
		groupGrid.setShowHeaderContextMenu(false);

        
		
		ListGridField thumbnail = new ListGridField("thumbnail", " ", 40);
		thumbnail.setAlign(Alignment.CENTER);
		thumbnail.setType(ListGridFieldType.IMAGE);
		thumbnail.setCanFilter(false);
		thumbnail.setCanSort(false);
		// thumbnail.setImageURLPrefix("flags/16/");
		// thumbnail.setImageURLSuffix(".png");

		ListGridField name = new ListGridField("name", "Name");
		name.setCanFreeze(false);

		ListGridField members = new ListGridField("members", "Members");
		members.setCanFilter(false);
		members.setWidth("55px");
		members.setAlign(Alignment.CENTER);
		
		groupGrid.setFields(thumbnail, name, members);
		groupGrid.setCanResizeFields(true);

		ds = GroupsDataSource.getInstance();
		groupGrid.setDataSource(ds);

		addMember(groupGrid);
	}

	public void setRecordClickHandler(RecordClickHandler handler) {
		groupGrid.addRecordClickHandler(handler);
	}

	@Override
	public void search(String string) {
		final LoadingWindow loadingWindow = new LoadingWindow();
		loadingWindow.show();
		
		pictureService.getGroups(string, 30, 1, Global.accessToken,
				new AsyncCallback<Collection<GroupModel>>() {

					@Override
					public void onSuccess(Collection<GroupModel> groups) {
						groupGrid.setData(new GroupListRecord[0]);
						groupGrid.fetchData();
						GroupListRecord[] records = new GroupListRecord[groups
								.size()];

						int i = 0;
						for (GroupModel gm : groups) {
							records[i++] = gm.getRecord();
						}

						ds.setTestData(records);
						loadingWindow.destroy();
					}

					@Override
					public void onFailure(Throwable caught) {
						loadingWindow.destroy();
						if (caught instanceof InvalidFlickrTokenException) {
							InvalidFlickrTokenException e = (InvalidFlickrTokenException) caught;
							Global.requestToken = e.getAuthorization()
									.getRequetToken();
							Notifier.notify(e.getAuthorization());
						}
					}
					
				});
	}
}
